package hisp_interface;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Identity{
	public String id;
	public String firstName;
	public String lastName;
	public String role;
	public String department;
	public String organization;
	
	public Identity() {
		id = "";
		firstName = "";
		lastName = "";
		role = "";
		department = "";
		organization = "";
	}
	
	public Identity(String xml) {
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(new ByteArrayInputStream(xml.getBytes()));
			List<Element> list = document.getRootElement().getChildren("identity");
			Element e = list.get(0);
			id = e.getAttributeValue("id");
			firstName = e.getAttributeValue("firstname");
			lastName = e.getAttributeValue("lastname");
			role = e.getAttributeValue("role");
			department = e.getAttributeValue("department");
			organization = e.getAttributeValue("organization");
		}
		catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getXMLString() {
		SAXBuilder builder = new SAXBuilder();
		try {
			Document document = builder.build(new ByteArrayInputStream("<root></root>".getBytes()));
			List<Element> list = document.getRootElement().getChildren();
			list.add(new Element("identity")
						.setAttribute("id", id)
						.setAttribute("firstname", firstName)
						.setAttribute("lastname", lastName)
						.setAttribute("role", role)
						.setAttribute("department", department)
						.setAttribute("organization", organization));
			String xml;
			XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			outputter.setFormat(Format.getCompactFormat());
			xml = outputter.outputString(document);
			return xml;				
		}
		catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
